<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }} <h3>{{bookName}}</h3></div>
    <div class="table-operator">
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <a-button
          class="fl-l"
          type="primary"
          @click='goBack'
        >返回</a-button>
        <a-button
          class="fl-l"
          type="primary"
          icon="plus"
          @click='handleCreate'
        >创建邀请码</a-button>
      </div>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <span  slot='used' slot-scope='text,item'>
        {{text}}/{{item.num*item.max}}
      </span>
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="text,item">
        <router-link :to="{ path: '/book/invite', query: { batchId: item.id,bookName:bookName } }">邀请码列表</router-link>
             <a v-action:makeCode
               @click='handleMakeCode(item.id)'
             >下载二维码</a>
      </span>

    </s-table>
    <makeInvite ref='form' @handleSubmit="handleRefresh(false)" />
    <makeCode ref='makeCode' />
  </a-card>
</template>

<script>
import * as Api from '@/api/book/invite'
import { STable } from '@/components'
import makeInvite from '@/views/book/modules/makeInvite'
import makeCode from '@/views/book/modules/makeCode'
import WorkTypeEnum from '@/common/enum/workType'

export default {
  name: 'Index',
  components: {
    STable,makeInvite,makeCode
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      bookId:0,
      bookName:'',
      // 表头
      columns: [
        {
          title: 'ID',
          dataIndex: 'id'
        },
        {
          title: '二维码数量',
          dataIndex: 'num'
        },
        {
          title: '最大绑定量',
          dataIndex: 'max'
        },
        {
          title: '已绑定量',
          dataIndex: 'used',
          scopedSlots: {customRender: 'used'}
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          title:'操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.batchList({ ...param, ...this.queryParam })
          .then(response => response.data.list)
      },
    }
  },
  created () {
    this.bookId = this.$route.query.bookId
    this.bookName = this.$route.query.bookName
    this.queryParam.bookId= this.bookId
  },
  methods: {
    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },
    handleCreate(){
      this.$refs.form.open(this.bookId)
    },
    goBack(){
      this.$router.go(-1)
    },
    handleMakeCode(id){
      this.$refs.makeCode.open(WorkTypeEnum.INVITE_QR.value,WorkTypeEnum.INVITE_QR_TEXT.value,id)
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
